<?php
session_start();
if(!isset($_SESSION['id'])){
    header('Location: login.php');
    exit;
}
include_once("../controller/predio_controller_class.php");
include_once("../controller/departamento_controller_class.php");
include_once("../controller/predio_departamento_controller_class.php");

$objPredio = new PredioController();

$listPredio = $objPredio->selecionar();

$objDepartamento = new DepartamentoController();

$listDepartamento = $objDepartamento->selecionar();

if(isset($_POST['salvar'])){
    @$predios = $_POST['chkPredio'];
    $departamento = $_POST['txtDepartamento'];
    if($departamento == 0){
        echo "<script>alert('Selecione o Departamento')</script>";
    }else{
        $objPredioD = new PredioDepartamentoController();
        if($objPredioD->cadastrar($predios, $departamento)){
            echo "<script>alert('OK')</script>";
        }else{
            echo "<script>alert('Erro')</script>";
        }
    }
}

?>
<!DOCTYPE html>
<html lang="en" class="no-js">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
        <title>::: SOS UNITAU -- Sistema de ordem de serviço</title>

        <link rel="shortcut icon" href="img/logo.jpg">
        <link rel="stylesheet" type="text/css" href="css/normalize.css" />
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
        <link rel="stylesheet" type="text/css" href="css/icons.css" />
        <link rel="stylesheet" type="text/css" href="css/style2.css" />
        <link rel="stylesheet" type="text/css" href="css/StylePage.css" />
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <link rel="stylesheet" type="text/css" href="css/component.css" />
        <script src="js/modernizr.custom.js"></script>
        <script src="js/jquery.min.js"></script>
        <script type="text/javascript" src="js/jquery1.6.min.js"></script>
        <script src="js/jquery.maskedinput-1.3.min.js"></script>
        <script type="text/javascript"><!-- PARA NAO CONFLITAR -->
            var $JQuery = jQuery.noConflict();
        </script>
        
        <script>
            $(document).ready(function(){
                $("#txtDepartamento").change(function(){
                    var id = $(this).val();
                    
                    $.post("../funcoes/departamentoPredio.php", {id: id}, function (data) {
                        $('#table_check').find('td').remove();
                        var i = 1;
                        $.each(data, function (key, value) {
//                            alert(value.id + " - " + value.descricao);
                            
                            if(i === 1){
                                $("#table_check").append("<tr>");
                            }
                            $('#table_check').append("<td> <input name='chkPredio[]' "+
                                value.checked +" class='chkPredio' value='"+ 
                                value.id +"' type='checkbox' />&nbsp; &nbsp;"+ value.nome +"</td>");
                            if(i%6 === 1){
                                $("#table_check").append("</tr>");
                                i = 1;
                            }else{
                                i++;
                            }
                        });
                    },'json').fail(function(){
                        alert("fail");
                    });
                });
            });
        </script>
        
        <script>
            $(document).ready(function () {
                $("#table_check").parent().parent().delegate('.chkPredio','change',function () {
                    var predios = new Array();

                    $(".chkPredio").each(function () {
                        if ($(this).is(":checked")) {
                            predios.push($(this).val());
                        }
                    });
                    if (predios.length == 0) {
                        predios.push("");
                    }
                    $.post("../funcoes/funcionariosPredios.php", {'predios[]': predios}, function (data) {
//                        alert(data);
//                        location.reload();
                        
                    }, 'json').fail(function () {
                        alert("fail");
                    });
                });
            });
        </script>



    </head>
    <!--------------------------------------------------------------------------------------------------->

    <body> <!-- Inicio -->
        <div class="loading"></div>
        <div class="container"> <!-- container -->



            <!-----------------------------CONTEUDO DA PÁGINA CADASTRO FUNCIONARIO --------------------------------->
            <!------- SEGUE ABAIXO ------->



            <div class="barra-superior"></div>
            <div class="sobre-empresa">
                Powered by: Grupo NOP
            </div>

            <!------------- CONTEUDO DE FORMULARIOS---------------------->

            <div class="titulo-page">
                ::: Configuração de departamentos
            </div>

            <form name="confDepartamento" id="confDepartamento" method="post" enctype="multipart/form-data" action="" class="style-form">

                <fieldset>
                    <legend>Lista Departamento</legend>
                    <label for="txtDepartamento">Departamento: </label>
                    <select name="txtDepartamento" id="txtDepartamento">
                        <option value="0">Selecione</option>
                        <?php foreach ($listDepartamento as $dep) { ?>
                            <option value="<?= $dep->getId(); ?>"><?= $dep->getDescricao(); ?></option>
                        <?php } ?>
                    </select>
                </fieldset>

                <fieldset>
                    <legend> Lista de Prédios </legend>

                    <table width="800px" class="table-form" id="table_check">
                        <?php if (count($listPredio) > 0) { ?>
                            <?php $i = 1; ?>
                            <?php foreach ($listPredio as $predio) { ?>
                                <?php
                                if ($i == 1) {
                                    echo "<tr>";
                                }
                                ?>
                                <td> 
                                    <input name="chkPredio[]" value="<?= $predio->getId() ?>" class="chkPredio" type="checkbox" />
                                    &nbsp; &nbsp;<?= $predio->getNome() ?>
                                </td>

                                <?php
                                if ($i % 6 == 0) {
                                    echo "</tr>";
                                    $i = 1;
                                }else{
                                    $i++;
                                }
                                ?>
                                <?php
                                
                            }
                        }
                        ?>


                    </table>

                </fieldset>

                <div class="clear"></div>   

<!--                <fieldset>
                    <legend>Funcionários por departamento </legend>
                    <table width="800px" class="table-form">
                        <tr>
                            <td>Nome:</td>
                            <td>Telefones:</td>
                            <td>Campus:</td>
                            <td>Predio:</td>
                            <td>Sala:</td>
                        </tr>
                    </table>

                </fieldset>-->

                <input type="submit" value="Salvar" name="salvar" style="display:inline-block; margin-left:75%; margin-top:3%;" class="botao" />
                <input type="reset" value="Cancelar" style="margin-left:5%;" class="botao" onclick=""/>    



            </form>

            <!------------- /CONTEUDO DE FORMULARIOS --------------------->

            <!-------------------------INCLUSÃO DE ITENS PARA  -------------------------------->







            <!-------------------------/INCLUSÃO DE ITENS PARA  -------------------------------->


            <!-- ATENÇÃO ATENÇÃO ATENÇÃO -->

         
            <?php include_once("menuLateral.php"); ?>
            <!-- ATENÇÃO ATENÇÃO ATENÇÃO -->





            <!-----------------------------------/FIM--------------------------------------------------------->
            <div class="border"> </div> <!-- Border rodapé -->

            <div class="barra-inferior"></div>



            <!------------------------------/CONTEUDO DA PÁGINA CADASTRO FUNCIONARIO ----------------------------------->



        </div><!-- /container -->
    </body><!-- Fim -->
    <!--------------------------------------------------------------------------------------------------->


   

<script src="js/classie.js"></script>
<script src="js/borderMenu.js"></script>

    <!-- /SCRIPTS POP-UP -->

</html>